package com.banksoft.cps.service.impl;

import com.banksoft.cps.bean.BuyCompany;
import com.banksoft.cps.bean.Company;
import com.banksoft.cps.bean.Register;
import com.banksoft.cps.bean.TransportCompany;
import com.banksoft.cps.bean.common.ErrorTip;
import com.banksoft.cps.bean.common.ServiceException;
import com.banksoft.cps.dao.BuyCompanyDao;
import com.banksoft.cps.dao.CompanyDao;
import com.banksoft.cps.dao.TransportCompanyDao;
import com.banksoft.cps.enums.AuditStatus;
import com.banksoft.cps.enums.CompanyStatus;
import com.banksoft.cps.enums.UserType;
import com.banksoft.cps.service.CompanyService;
import com.banksoft.cps.service.RegisterService;
import com.banksoft.cps.service.TransportCompanyService;
import com.banksoft.cps.util.UUIDFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.ws.soap.Addressing;

/**
 * Created by rongzhao.zhu on 2018/8/31.
 */
@Service
public class RegisterServiceImpl implements RegisterService {

    @Autowired
    CompanyDao companyDao;
    @Autowired
    TransportCompanyDao transportCompanyDao;
    @Autowired
    BuyCompanyDao buyCompanyDao;
    @Override
    public void register(Register register) throws ServiceException {
        long now  = System.currentTimeMillis();
        if(UserType.company.name().equals(register.getUserType())){
            Company  company =  companyDao.findByAccount(register.getAccount());
            if(company !=null ){
                throw new ServiceException(1000,"该用户名已经被注册!");
            }else {
                Company bean = new Company();
                bean.setId(UUIDFactory.random());
                bean.setName("");
                bean.setStatus(CompanyStatus.inProduction.name());
                bean.setCreateTime(now);
                bean.setAccount(register.getAccount());
                bean.setPassword(register.getPassword());
                bean.setAuditStatus(AuditStatus.create.name());
                companyDao.insert(bean);
            }

        }else if(UserType.trans.name().equals(register.getUserType())){
            TransportCompany transportCompany =  transportCompanyDao.findByAccount(register.getAccount());
            if(transportCompany !=null ){
                throw new ServiceException(1000,"该用户名已经被注册!");
            }else {
                TransportCompany bean = new TransportCompany();
                bean.setId(UUIDFactory.random());
                bean.setName("");
                bean.setCreateTime(now);
                bean.setStatus(CompanyStatus.inProduction.name());
                bean.setAccount(register.getAccount());
                bean.setPassword(register.getPassword());
                bean.setAuditStatus(AuditStatus.create.name());
                transportCompanyDao.insert(bean);
            }
        }else if(UserType.buy.name().equals(register.getUserType())){
            BuyCompany buyCompany = buyCompanyDao.findByAccount(register.getAccount());
            if(buyCompany !=null ){
                throw new ServiceException(1000,"该用户名已经被注册!");
            }else {
                BuyCompany bean = new BuyCompany();
                bean.setId(UUIDFactory.random());
                bean.setName("");
                bean.setCompanyId("");
                bean.setCreateTime(now);
                bean.setStatus(CompanyStatus.inProduction.name());
                bean.setAuditStatus(AuditStatus.create.name());
                bean.setAccount(register.getAccount());
                bean.setPassword(register.getPassword());
                bean.setAuditStatus(AuditStatus.create.name());
                buyCompanyDao.insert(bean);
            }
        }
    }
}
